package com.dh.service.impl;

import com.dh.core.config.Constant;
import com.dh.entity.User;
import com.dh.entity.UserRP;
import com.dh.entity.vo.UserRPVO;
import com.dh.repository.UserRPRepository;
import com.dh.service.UserRPService;
import com.dh.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Service
public class UserRPServiceImpl implements UserRPService {

    @Autowired
    private UserRPRepository rpRepository;

    @Autowired
    private UserService userService;

    @Override
    public Page<UserRPVO> page(PageRequest pageRequest) {
        return rpRepository.findByDeleted(Constant.NOT_DELETE, pageRequest).map(rp -> {
            UserRPVO vo = new UserRPVO();
            BeanUtils.copyProperties(rp, vo);
            vo.setUser(userService.get(rp.getUserId()));
            return vo;
        });
    }

    @Override
    public UserRP get(Long rpId) {
        return rpRepository.findOne(rpId);
    }

    @Override
    public void delete(Long rpId) {
//        UserRP rp = rpRepository.getOne(rpId);
//        rp.setDeleted(Constant.DELETE);
//        rpRepository.save(rp);
        rpRepository.delete(rpId);
    }

    @Override
    public void create(UserRP rp) {
        rpRepository.save(rp);
    }

    @Override
    public void update(UserRP rp) {
        rpRepository.save(rp);
    }

    @Override
    public UserRP findByNoAndDeleted(String no, byte deleted) {
        return rpRepository.findByNoAndDeleted(no,deleted);
    }

    @Override
    public void deleteByUserId(Long userId) {
        rpRepository.deleteByUserId(userId);
    }


}
